﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using EventCallBack;
namespace Yu
{
    public enum ActorType
    {
        Center
    }

    /// <summary>
    /// 此脚本用来排列组合子方块
    /// </summary>
    public class ItemGroup : MonoBehaviour
    {
        public static ItemGroup Instance;
        public Vector2 ItemSize = new Vector2(100, 100);//默认图标的尺寸
        public float itemsDir = 0.1f;//方块间的间距
        public Vector2 ScreenSize;//屏幕大小
        public Vector2 Zero;
        public GameObject Image;
        public int ItemMoveSpeed = 30;
        public int Range = 400;
        public int TargetImageCD = 60;
        public static List<TargetImage> targetPoint = new List<TargetImage>();
        public float FirstPos;
        public float LastPos;

        private int hang = 0;
        private int lie = 0;

        private void Awake()
        {
            Instance = this;
            ScreenSize = GameObject.Find("MainCanvas").GetComponent<RectTransform>().sizeDelta;
        }

        void Start()
        {
            itemsDir = (ScreenSize.y % ItemSize.y )/ (ScreenSize.y / ItemSize.y);
            if (itemsDir<5)
            {
                itemsDir = 5;
            }
            Zero = new Vector2(-ScreenSize.x / 2 + ItemSize.x/2, -ScreenSize.y/2 + ItemSize.y / 2);
            hang = (int)(ScreenSize.x / (ItemSize.x + itemsDir)) + 3;
            lie = (int)(ScreenSize.y / (ItemSize.y + itemsDir));
            Debug.Log(hang);
            FirstPos = Zero.x - (itemsDir + ItemSize.x);
            LastPos = Zero.x + (hang-1) * (itemsDir + ItemSize.x);
            Debug.Log("FirstPos" + FirstPos + ":" + "LastPos" + LastPos);
            for (int i = 0; i < hang; i++)
            {
                for (int j = 0; j < lie; j++)
                {
                    CreateItem(new Vector2(Zero.x- (itemsDir + ItemSize.x) + (itemsDir + ItemSize.x) * i, Zero.y + itemsDir + (itemsDir + ItemSize.y) * j), ItemSize);
                }
            }
        }

        private void CreateItem(Vector2 pos,Vector2 Size)
        {
            GameObject objTemp = Instantiate(Image);
            objTemp.transform.parent = transform;
            RectTransform rectTransform = objTemp.GetComponent<RectTransform>();
            rectTransform.sizeDelta = Size;
            rectTransform.localPosition = pos;
        }
    }
}